﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Services;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.UI.Application.ActionResults;

namespace MethodWorx.CMS.UI.Areas.Application.Controllers
{
    public class ContentItemController : Controller
    {
        private IContentService contentService;
        //
        // GET: /Application/ContentItem/
        public ContentItemController(IContentService contentService)
        {
            if (contentService == null)
                throw new ArgumentNullException("contentService");

            this.contentService = contentService;
        }

        [HttpGet]
        public ActionResult Rename(ContentItem contentItem)
        {
            return View(contentItem);
        }

        public ActionResult Resequence(Page page, Guid[] sequence)
        {
            return new EmptyResult();
        }

        [HttpPost]
        public ActionResult Rename(ContentItem contentItem, string newName)
        {
            if (string.IsNullOrEmpty(newName))
                this.ViewData["error"] = "Please enter a new name";
            else
            {
                try
                {
                    this.contentService.RenameContentItem(contentItem, newName);
                    return new CloseDialogResult("ok");
                }
                catch (BusinessRuleException ex)
                {
                    this.ViewData["error"] = ex.Message;
                }
            }

            return View(contentItem);
        }

    }
}
